package com.zhengsr.wanandroid.mvp.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.zhengsr.corelib.utils.Lgger;
import com.zhengsr.wanandroid.R;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.fragmentation.SupportActivity;
import qiu.niorgai.StatusBarCompat;

/**
 * Created by zhengshaorui
 * time: 2018/8/19
 */

public abstract  class BaseActivity<T extends BasePresent>
        extends SupportActivity implements IBaseView {
    private static final int NORMAL_VIEW = 1;
    private static final int LOADING_VIEW = 2;
    private static final int ERROR_VIEW = 3;

    private View mLoadingView;
    private View mNormalView;
    private View mErrorView;
    private T mPresent;
    private Unbinder mUnbinder;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FrameLayout parentView = (FrameLayout) LayoutInflater.from(this).inflate(R.layout.frame_base,null);
        setContentView(parentView);

        configView(parentView);
        mUnbinder = ButterKnife.bind(this,mNormalView);
        mPresent = getPresent();
        if (mPresent != null){
            mPresent.attachView(this);
        }
        StatusBarCompat.setStatusBarColor(this,
                this.getResources().getColor(R.color.maincolor));
        initView();

    }

    private void configView(FrameLayout parentView) {
        mNormalView = LayoutInflater.from(this).inflate(getLayoutId(),parentView,false);
        mLoadingView = LayoutInflater.from(this).inflate(R.layout.loadingview_layout,parentView,false);

        mErrorView = LayoutInflater.from(this).inflate(R.layout.load_error_layout,parentView,false);

        mErrorView.findViewById(R.id.error_reload_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reload();
            }
        });
        parentView.addView(mNormalView);
        parentView.addView(mLoadingView);
        parentView.addView(mErrorView);
        if (showLoadingView()){
            showCurrentView(LOADING_VIEW);
        }else{
            showCurrentView(NORMAL_VIEW);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresent != null){
            mPresent.detachView();
            mPresent = null;
        }
        if (mUnbinder != null){
            mUnbinder.unbind();
        }
    }
    protected boolean showLoadingView(){
        return false;
    }


    public abstract int getLayoutId();
    public abstract T getPresent();
    public abstract void initView();

    @Override
    public void showLoading() {
        showCurrentView(LOADING_VIEW);
    }

    @Override
    public void loadSuccess() {
        showCurrentView(NORMAL_VIEW);
    }

    @Override
    public void showError() {
        showCurrentView(ERROR_VIEW);
    }

    private void showCurrentView(int type){
        if (mErrorView == null || mNormalView == null || mLoadingView == null){
            return;
        }

        mNormalView.setVisibility(View.GONE);
        mLoadingView.setVisibility(View.GONE);
        mErrorView.setVisibility(View.GONE);

        if (type == NORMAL_VIEW){
            mNormalView.setVisibility(View.VISIBLE);
        }else if (type == LOADING_VIEW){
            mLoadingView.setVisibility(View.VISIBLE);
        }else if (type == ERROR_VIEW){
            mErrorView.setVisibility(View.VISIBLE);
        }
    }


    @Override
    public void useNightMode(boolean isNightMode) {

    }
    @Override
    public void showErrorMsg(String msg) {
        Lgger.d("zsr --> base error msg: "+msg);
    }
    public void reload(){

    }
}
